<?php

namespace Platform\Models\User;

use Platform\Models\BaseModel;

class User extends BaseModel {

    const TABLE_NAME = 'user';

    public function initialize() {
        parent::initialize();
        $this->setSource(self::TABLE_NAME);
    }

    private $id;
    private $unionId;
    private $nickname;
    private $img;
    private $minibegMoney;
    private $minisendMoney;
    private $money;
    private $city;
    private $province;
    private $country;
    private $sex;
    private $createTime;
    private $minisendCount;
    private $minibegCount;
    private $qmzfOpenId;

    public function getId() {
        return $this->id;
    }

    public function getUnionId() {
        return $this->unionId;
    }

    public function getNickname() {
        return $this->nickname;
    }

    public function getImg() {
        return $this->img;
    }

    public function getMinibegMoney() {
        return $this->minibegMoney;
    }

    public function getMinisendMoney() {
        return $this->minisendMoney;
    }

    public function getMoney() {
        return $this->money;
    }

    public function getCity() {
        return $this->city;
    }

    public function getProvince() {
        return $this->province;
    }

    public function getCountry() {
        return $this->country;
    }

    public function getSex() {
        return $this->sex;
    }

    public function getCreateTime() {
        return $this->createTime;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setUnionId($unionId) {
        $this->unionId = $unionId;
    }

    public function setNickname($nickname) {
        $this->nickname = $nickname;
    }

    public function setImg($img) {
        $this->img = $img;
    }

    public function setMinibegMoney($minibegMoney) {
        $this->minibegMoney = $minibegMoney;
    }

    public function setMinisendMoney($minisendMoney) {
        $this->minisendMoney = $minisendMoney;
    }

    public function setMoney($money) {
        $this->money = $money;
    }

    public function setCity($city) {
        $this->city = $city;
    }

    public function setProvince($province) {
        $this->province = $province;
    }

    public function setCountry($country) {
        $this->country = $country;
    }

    public function setSex($sex) {
        $this->sex = $sex;
    }

    public function setCreateTime($createTime) {
        $this->createTime = $createTime;
    }

    public function getMinisendCount() {
        return $this->minisendCount;
    }

    public function setMinisendCount($minisendCount) {
        $this->minisendCount = $minisendCount;
    }

    public function getMinibegCount() {
        return $this->minibegCount;
    }

    public function setMinibegCount($minibegCount) {
        $this->minibegCount = $minibegCount;
    }

    public function getQmzfOpenId() {
        return $this->qmzfOpenId;
    }

    public function setQmzfOpenId($qmzfOpenId) {
        $this->qmzfOpenId = $qmzfOpenId;
    }

    public function columnMap() {
        //列名 =》 属性名
        return [
            'id' => 'id',
            'union_id' => 'unionId',
            'nickname' => 'nickname',
            'img' => 'img',
            'minibeg_money' => 'minibegMoney',
            'minisend_money' => 'minisendMoney',
            'money' => 'money',
            'city' => 'city',
            'province' => 'province',
            'country' => 'country',
            'sex' => 'sex',
            "create_time" => "createTime",
            "minisend_count" => "minisendCount",
            "minibeg_count" => "minibegCount",
            "qmzf_open_id" => "qmzfOpenId"
        ];
    }

    /**
     * 根据UnionId获取用户资料
     * @param type $phone
     */
    public function getUserByUnionId($unionId, $columns = '', $returnObject = false) {
        $query = [
            "conditions" => "unionId = :unionId:",
            "bind" => [
                "unionId" => $unionId
            ],
        ];
        if ($columns) {
            $query['columns'] = $columns;
        }
        $user = self::findFirst($query);
        if ($user && $returnObject == false) {
            $user = $user->toArray();
        }
        return $user;
    }

    /**
     * 获取一条加锁的数据
     * @param $id
     * @param $columns
     */
    public function getForUpdateDataById($id, $columns = '') {
        $query = [
            "conditions" => "$this->pk = :id:",
            "bind" => [
                "id" => $id
            ],
            "for_update" => true
        ];
        if ($columns) {
            $query['columns'] = $columns;
        }
        $data = self::findFirst($query);
        return $data;
    }

}
